package service;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import database.Conn;

public class Statistic {
	Connection connect =null;
	PreparedStatement pstm=null;
	private String table="tblStatis";
	public boolean AddStatistic(int date,int number_room,int number_customer, int revenue){
		connect = Conn.getConnect();
		try{
			pstm= connect.prepareStatement("INSERT INTO "+this.table+"(date,n_room," +
					"n_customer,revenue) VALUES (?,?,?,?)");
		pstm.setInt(1,date);
		pstm.setInt(2,number_room);
		pstm.setInt(3,number_customer);
		pstm.setInt(4,revenue);
			if(pstm.executeUpdate()>0){
				return true;
			}
			
		}catch(SQLException e){
			
		}
		return false;
		
	}
	public boolean DeleteStatisic(int id){
		connect = Conn.getConnect();
		try{
			pstm = connect.prepareStatement("DELETE FROM "+this.table+ " WHERE id = ?");
			pstm.setInt(1, id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public boolean UpdateStatisic(int id,int date,int number_room,int number_customer, int revenue){
		connect= Conn.getConnect();
		try{
			pstm = connect.prepareStatement("UPDATE "+this.table +"SET date=?,n_room?," +
					"n_customer=?,revenue=?  WHERE id=?");
			pstm.setInt(1,date);
			pstm.setInt(2,number_room);
			pstm.setInt(3,number_customer);
			pstm.setInt(4,revenue);
			pstm.setInt(5,id);
			if(pstm.executeUpdate()>0){
				return true;
			}
		}catch(SQLException e){
			
		}
		return false;
	}
	public ArrayList<entity.Statistic> GetAllStatistic(){
		connect= Conn.getConnect();
		ResultSet rs=null;
		ArrayList<entity.Statistic> list= new ArrayList<entity.Statistic>();
		try{
			pstm= connect.prepareStatement("Select *from "+this.table);
			rs=pstm.executeQuery();
			while(rs.next()){
				entity.Statistic use= new entity.Statistic();
				use.setId(rs.getInt("id"));
				use.setDate(rs.getInt("date"));
				use.setNumber_customer(rs.getInt("n_room"));
				use.setNumber_customer(rs.getInt("n_customer"));
				use.setRevenue(rs.getInt("revenue"));
				list.add(use);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return list;
	}
	public void GetStatisicById(int id){
		
		connect= Conn.getConnect();
		ResultSet rs=null;
		try{
			pstm= connect.prepareStatement("Select *from "+this.table+" where id=?");
			pstm.setInt(1, id);
			rs= pstm.executeQuery();
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
	
}
